//
//  GameSDKApi.m
//  unity-plugin
//
//  Created by hsy on 2018/10/17.
//

#import "GameSDKApi.h"

@implementation GameSDKApi

+ (GameSDKApi*)shareInstance{
    static GameSDKApi *gameSDKApi =nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        gameSDKApi = [[self alloc] init];
    });
    return gameSDKApi;
}

- (void) callBackUnity:(NSString *)obj method:(NSString *)method msg:(NSString *)msg{
    NSLog(@"obj:%@",obj);
    NSLog(@"method:%@",method);
    NSLog(@"msg:%@",msg);
    UnitySendMessage([obj UTF8String], [method UTF8String], [msg UTF8String]);
}

- (void) login{
    UIWindow *window = [[UIApplication sharedApplication] keyWindow];
    //初始化
    _loginAPI = [LoginAPI shareManager];
    //登录界面

    UIViewController *vc = [_loginAPI getLoginViewController:^(NSString *data, BOOL flag) {
        NSLog(@"flag:%@",flag ? @"YES" : @"NO");
        NSLog(@"data:%@",data);

        NSString *loginObject = [GameSDKApi shareInstance].loginObject;

        NSMutableDictionary *loginDict = [NSMutableDictionary dictionary];
        if(flag){
            NSDictionary * loginDataDict = [NSJSONSerialization JSONObjectWithData:[data dataUsingEncoding:NSUTF8StringEncoding] options:0 error:nil];
            [loginDict setValue:@"登录成功" forKey:@"msg"];
            [loginDict setValue:[NSNumber numberWithInt:LOGIN_SUCCESS] forKey:@"ret"];
            [loginDict setValue:loginDataDict forKey:@"data"];
        }else{
            [loginDict setValue:@"登录失败" forKey:@"msg"];
            [loginDict setValue:[NSNumber numberWithInt:LOGIN_FAIL] forKey:@"ret"];
            [loginDict setValue:@"登录失败" forKey:@"data"];
        }
        NSData * jsonData = [NSJSONSerialization dataWithJSONObject:loginDict options:0 error:nil];
        NSString *jsonStr = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
        NSLog(@"jsonStr:%@",jsonStr);

        [[GameSDKApi shareInstance] callBackUnity:loginObject method:LOGIN_RESULT msg:jsonStr];
    }];

    if([[[UIDevice currentDevice] systemVersion] floatValue]>=8.0) {
        vc.modalPresentationStyle = UIModalPresentationOverCurrentContext;
    }else{
        vc.modalPresentationStyle = UIModalPresentationCurrentContext;
    }
    //[self presentViewController:vc animated:YES completion:^{}];
    [window.rootViewController presentViewController:vc animated:YES completion:nil];
}

//sdk操作退出游戏
- (void) gameLogout{
    NSLog(@"gameLogout");
    NSString *loginObject = [GameSDKApi shareInstance].loginObject;
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setValue:@"登出成功" forKey:@"msg"];
    [dict setValue:[NSNumber numberWithInt:LOGOUT] forKey:@"ret"];
    [dict setValue:@"登出成功" forKey:@"data"];

    NSData * jsonData = [NSJSONSerialization dataWithJSONObject:dict options:0 error:nil];
    NSString *jsonStr = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];

    [[GameSDKApi shareInstance] callBackUnity:loginObject method:LOGIN_RESULT msg:jsonStr];
}

- (void)setOnLoginListener:(NSString *)gameObject{
    [GameSDKApi shareInstance].loginObject = gameObject;

    [_loginAPI setOnLogoutListener:^(BOOL isSuccess){
        NSLog(@"setOnLogoutListener");
        [[GameSDKApi shareInstance] gameLogout];
    }];
}

//游戏操作退出游戏
- (void)logout{
    [_loginAPI logout];
}

- (void)pay:(NSString *)payJson gameObject:(NSString *)gameObject{
    //    NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
    //                           @"msg", @"支付成功",
    //                           @"ret", [NSNumber numberWithInt:PAY_SUCCESS],
    //                           @"data", @"支付成功",
    //                           nil];

    NSString *objStr = gameObject;

    [_loginAPI webpay:payJson payvcblock:^(UIViewController *vc) {
        if (vc) {
            
            if([[[UIDevice currentDevice] systemVersion] floatValue]>=8.0) {
                vc.modalPresentationStyle = UIModalPresentationOverCurrentContext;
            }else{
                vc.modalPresentationStyle = UIModalPresentationCurrentContext;
            }
            //[self presentViewController:vc animated:YES completion:^{}];
            UIWindow *window = [[UIApplication sharedApplication] keyWindow];
            [window.rootViewController presentViewController:vc animated:YES completion:^{}];
        }
    } payresultblock:^(BOOL isSucess) {
        NSMutableDictionary *dict = [NSMutableDictionary dictionary];
        if (isSucess){
            NSLog(@"pay ok....");
            [dict setValue:@"支付成功" forKey:@"msg"];
            [dict setValue:[NSNumber numberWithInt:PAY_SUCCESS] forKey:@"ret"];
            [dict setValue:@"支付成功" forKey:@"data"];
        }
        else{
            NSLog(@"pay false....");
            [dict setValue:@"支付失败" forKey:@"msg"];
            [dict setValue:[NSNumber numberWithInt:PAY_FAIL] forKey:@"ret"];
            [dict setValue:@"支付失败" forKey:@"data"];
        }
        
        NSData * jsonData = [NSJSONSerialization dataWithJSONObject:dict options:0 error:nil];
        NSString *jsonStr = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
        
        [[GameSDKApi shareInstance] callBackUnity:objStr method:PAY_RESULT msg:jsonStr];
    }];
    
}

- (void)checkPay:(NSString *)gameObject{
    NSString *objStr = gameObject;
    [_loginAPI payState:^(NSString *data, BOOL flag) {

        NSLog(@"snyc data: %@", data);

        NSDictionary * checkPayDataDict = [NSJSONSerialization JSONObjectWithData:[data dataUsingEncoding:NSUTF8StringEncoding] options:0 error:nil];

        NSMutableDictionary *dict = [NSMutableDictionary dictionary];
        //NSString *data = @"{\"pay_state\":0}";
        [dict setValue:checkPayDataDict forKey:@"data"];

        if(flag){
            NSLog(@"yes");
            [dict setValue:@"检测支付成功" forKey:@"msg"];
            [dict setValue:[NSNumber numberWithInt:CHECK_PAY_SUCCESS] forKey:@"ret"];

        }else{
            NSLog(@"no");
            [dict setValue:@"检测支付失败" forKey:@"msg"];
            [dict setValue:[NSNumber numberWithInt:CHECK_PAY_FAIL] forKey:@"ret"];
        }

        NSData * jsonData = [NSJSONSerialization dataWithJSONObject:dict options:0 error:nil];
        NSString *jsonStr = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];

        [[GameSDKApi shareInstance] callBackUnity:objStr method:CHECK_PAY_RESULT msg:jsonStr];
    }];
}

- (void)syncData:(NSString *)dataJson gameObject:(NSString *)gameObject{
    NSString *objStr = gameObject;
  
    [_loginAPI syncData:dataJson syncBlock:^(NSString *msg,BOOL flag)
     {
         NSMutableDictionary *dict = [NSMutableDictionary dictionary];
         if(flag){
             [dict setValue:@"sync success" forKey:@"msg"];
             [dict setValue:[NSNumber numberWithInt:SYNC_INFO_SUCCESS] forKey:@"ret"];
             [dict setValue:@"sync success" forKey:@"data"];
             NSLog(@"snyc data success: %@", msg);
         }else{
             [dict setValue:@"sync fail" forKey:@"msg"];
             [dict setValue:[NSNumber numberWithInt:SYNC_INFO_FAIL] forKey:@"ret"];
             [dict setValue:@"sync fail" forKey:@"data"];
             NSLog(@"snyc data fail: %@", msg);
         }

         NSData * jsonData = [NSJSONSerialization dataWithJSONObject:dict options:0 error:nil];
         NSString *jsonStr = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
         [[GameSDKApi shareInstance] callBackUnity:objStr method:SYNC_RESULT msg:jsonStr];
     }];
}

- (void)exitGame{
    [_loginAPI exitGame];
}



@end
